package com.gf.search.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.base.type.IndexType;
import com.gf.common.bean.Page;
import com.gf.search.dao.SearchDao;

@Service("searchDao")
public class SearchDaoImpl extends BaseDaoImpl implements SearchDao {

	/**
	 * select * from user where nickname like ?
	 */
	@Value("${searchdao.searchForUserSql}")
	private String searchForUserSql;
	
	/**
	 * select * from question where title like ?
	 */
	@Value("${searchdao.searchForHelpSql}")
	private String searchForHelpSql;
	
	/**
	 * select * from exam where title like ?
	 */
	@Value("${searchdao.searchForExamSql}")
	private String searchForExamSql;

	@Override
	public <T> List<T> search(String keyword, IndexType indexType, Page page , RowMapper<T> rowMapper) {
		
		
		//用户昵称
		if(indexType.equals(IndexType.USER)){
			return getPaginationResult(searchForUserSql, page, rowMapper, "%"+keyword+"%");
		}
		
		else if(indexType.equals(IndexType.HELP)){
			return getPaginationResult(searchForHelpSql, page, rowMapper, "%"+keyword+"%");
		}
		
		else if(indexType.equals(IndexType.EXAM)){
			return getPaginationResult(searchForExamSql, page, rowMapper, "%"+keyword+"%");
		}
		
		return new ArrayList<T>();
	}
}
